package my_utils

import (
	"crypto/md5"
	"crypto/sha512"
	"encoding/hex"
	"github.com/anaskhan96/go-password-encoder"
	"io"
)

func genMd5(code string) string {
	Md5 := md5.New()
	_, _ = io.WriteString(Md5, code)
	return hex.EncodeToString(Md5.Sum(nil))
}

var options = &password.Options{16, 100, 32, sha512.New}

// 生成随机盐值和加密密码
func GenEncodePassword(pwd string) (salt string, encodedPwd string) {
	// Using custom options 自定义盐值
	salt, encodedPwd = password.Encode(pwd, options)
	return
}
func VerifyPwd(rawPwd string, salt string, encodedPwd string) (check bool) {
	check = password.Verify(rawPwd, salt, encodedPwd, options)
	return
}

//options := &password.Options{16, 100, 32, sha512.New}
//salt, encodedPwd := password.Encode("generic password", options)
//newPassword := fmt.Sprintf("$pbkdf2-sha512$%s$%s", salt, encodedPwd)
//fmt.Println(len(newPassword))
//fmt.Println(newPassword)
//
//passwordInfo := strings.Split(newPassword, "$")
//fmt.Println(passwordInfo)
//check := password.Verify("generic password", passwordInfo[2], passwordInfo[3], options)
//fmt.Println(check) // true
